package com.kermel.common.util;

import android.app.Activity;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.provider.MediaStore;

public class RingtoneUtils {
    public static final int PROMPT_FOR_RINGTONE_SELECTION_ID = 123;
    
    public static Ringtone obtainRingtone(Context context, String ringtoneUriString) {
        Ringtone ringtone = null;
        
        
        if (ringtoneUriString != null) {
            Uri ringtoneUri = Uri.parse(ringtoneUriString);
            ringtone = RingtoneManager.getRingtone(context, ringtoneUri);
        }
        
        return ringtone;
    }
    
    public static void promptForRingtoneSelection(Activity activity) {
        if (activity != null) {
            Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
            intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_ALL);
            
            Uri uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 1l);
            intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
            intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, uri);
            activity.startActivityForResult(intent, PROMPT_FOR_RINGTONE_SELECTION_ID);
        }
    }
    
    public static String getRingtoneTitleFromUri(Context context, Uri ringtoneUri) {
        String title = StringUtils.EMPTY;
        
        Ringtone ringtone = RingtoneManager.getRingtone(context, ringtoneUri);
        
        if (ringtone != null)
            title = ringtone.getTitle(context);
        
        return title;
    }
}
